package com.jade.cloud.store.service.impl;

import com.jade.bss.DbException;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.zsy.base.dao.EmployeeDao;
import com.jade.zsy.base.dao.StoreDao;
import com.jade.zsy.base.entity.Employee;
import com.jade.zsy.store.dao.StoreEmployeeDao;
import com.jade.zsy.base.entity.Store;
import com.jade.zsy.store.entity.StoreEmployee;
import com.jade.zsy.store.service.StoreEmployeeFacade;
import com.jade.zsy.store.vo.StoreEmployeeVo;
import com.jade.zsy.base.vo.StoreVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2016-4-17
 * Time: 13:45
 * Version : 1.0
 * Remark:
 * History:
 */
@Service("storeEmployeeFacade")
public class StoreEmployeeFacadeImpl extends BaseFacadeImpl implements StoreEmployeeFacade {
    @Inject
    @Named("StoreEmployeeDao")
    private StoreEmployeeDao storeEmployeeDao;
    @Inject
    @Named("StoreDao")
    private StoreDao storeDao;
    @Inject
    @Named("EmployeeDao")
    private EmployeeDao employeeDao;

    public PagingResult getStoreByEmployee(Object obj, int page, int size, String keys, String... orders) throws DbException,IllegalAccessException, InstantiationException {
        StoreEmployeeVo entity=(StoreEmployeeVo)obj;
        Criteria criteria = new Criteria((page - 1) * size, size);
        criteria.add(Restrictions.eq("employeeId", entity.getEmployeeid()));
        PagingList<StoreEmployee> list = storeEmployeeDao.queryEntity(StoreEmployee.class,criteria);
        if(list!=null && list.size()>0){
            Long[] storeIds=new Long[list.size()];
            for(int i=0;i<list.size();i++){
                storeIds[i]=list.get(i).getEmployeeId();
            }
            Criteria resultCriteria = new Criteria();
            resultCriteria.add(Restrictions.in("id", storeIds));
            PagingList<Store> resultList = storeDao.queryEntity(Store.class,resultCriteria);
            PagingResult result = PagingResult.getResult(CopyBean.copy(resultList, StoreVo.class), resultList.getTotalCount(), page, size);
            return result;
        }
        return new PagingResult();
    }

    @Transactional
    public List<Employee> getEmployee(long storeId)throws DbException{
        PagingList<StoreEmployee> pagingList = super.queryEntity(StoreEmployee.class,new Criteria().add(Restrictions.eq("storeId",storeId)));
        List list = new ArrayList();
        for (StoreEmployee o : pagingList) {
            list.add(o.getEmployeeId());
        }
        return employeeDao.getByIds(Employee.class,list.toArray());
    }
}
